﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Resn.WhuPub.BIL;
using Resn.WhuPub.BLL;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// CreateCorporateBranchWindow.xaml 的交互逻辑
        /// </summary>
        public partial class CreateCorporateBranchPage : Page
        {
                private readonly GlobalManager gm = GlobalManager.Instance;

                private readonly Dictionary<string, List<string>> provinceCitys = ProvinceManager.Instance.ProvinceCitys;

                public CreateCorporateBranchPage()
                {
                        InitializeComponent();
                }

                public String CorporateBranchName { get; set; }

                public String CorporateBranchProvince { get; set; }

                public String CorporateBranchCity { get; set; }

                public String CorporateBranchAddress { get; set; }

                public String CorporateBranchPostcode { get; set; }

                public String CorporateBranchWebsite { get; set; }


                private void Page_Loaded(object sender, RoutedEventArgs e)
                {
                        LayoutRoot.DataContext = this;

                        CorporateNameTextBlock.Text = gm.Corporate.Name;
                        CorporateLevelTextBlock.Text = gm.Corporate.CorporateLevel.ToString();

                        foreach (string provinceCity in provinceCitys.Keys)
                        {
                                CorporateBranchProvinceComboBox.Items.Add(provinceCity);
                        }


                        CorporateBranchProvinceComboBox.SelectedIndex = 0;
                }

                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        if (String.IsNullOrEmpty(CorporateBranchName))
                        {
                                MessageBox.Show("学校名称不能为空");
                                return;
                        }

                        try
                        {
                                ICorporateBranch corporateBranch =
                                        gm.Corporate.CreateCorporateBranch(CorporateBranchName,
                                                                                  CorporateBranchAddress,
                                                                                  CorporateBranchPostcode,
                                                                                  CorporateBranchProvince,
                                                                                  CorporateBranchCity,
                                                                                  CorporateBranchWebsite);
                                corporateBranch.Insert();
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        NavigationService.Navigate(new Uri("ManageCorporateBranchsPage.xaml", UriKind.Relative));
                }


                private void CancelButton_Click(object sender, RoutedEventArgs e)
                {
                        NavigationService.Navigate(new Uri("ManageCorporateBranchsPage.xaml", UriKind.Relative));
                }

                private void CorporateBranchProvinceComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
                {
                        if (CorporateBranchProvinceComboBox.SelectedIndex != -1)
                        {
                                CorporateBranchCityComboBox.Items.Clear();

                                foreach (
                                        string provinceCity in
                                                provinceCitys[CorporateBranchProvinceComboBox.SelectedValue.ToString()])
                                {
                                        CorporateBranchCityComboBox.Items.Add(provinceCity);
                                }
                                CorporateBranchCityComboBox.SelectedIndex = 0;
                        }
                }
        }
}